import matplotlib.pyplot as plt

import pandas as pd
# 1. 创建Series
s1 = pd.Series([10,20,30,40], index=["a","b","c","d"])
print("=== Series ===")
print(s1, "\n索引：", s1.index, " 值：", s1.values)
# 2. 创建DataFrame
data = {
    "姓名": ["张三","李四","王五","赵六","钱七"],
    "年龄": [18,19,18,20,19],
    "语文成绩": [85,92,78,90,88],
    "数学成绩": [90,85,82,95,86]
}
df = pd.DataFrame(data)


# 中文字体设置（避免乱码）
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False
print("Matplotlib配置完成")

# 数据准备
students = df["姓名"]
math_scores = df["数学成绩"]
# 绘制折线图
plt.figure(figsize=(10,6))  # 画布10×6英寸
plt.plot(students, math_scores, marker='o', linewidth=2, color='#1f77b4', label='数学成绩')
# 样式设置
plt.title('学生数学成绩折线图', fontsize=14, pad=20)
plt.xlabel('学生姓名', fontsize=12)
plt.ylabel('数学成绩（分）', fontsize=12)
plt.legend(loc='upper right', fontsize=10)
plt.grid(True, alpha=0.3)
# 显示图表（内嵌）
plt.show()
# 保存图表
plt.savefig('math_line.png', dpi=300, bbox_inches='tight')
print("图表保存至笔记本目录")